//
//  ZXXComposeCollectionViewCell.swift
//  M-Weibo
//
//  Created by ZXX on 16/4/24.
//  Copyright © 2016年 ZXX. All rights reserved.
//

import UIKit

///重用cell 标示符
let ZXXComposePictureCellReuseIdentifier = "ZXXComposePictureViewCellReuseIdentifier"



class ZXXComposePictureView: UICollectionView {
    
    ///数据源数组
    var imagebgArray: [UIImage] = [UIImage]()
    /// 告知外界弹出选择图片控制器
    var ComposePictureViewClosure: (()->())?
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
//        设置UI
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   

    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
//        刚开始的时候是隐藏的
        self.hidden = true
//        注册cell
        registerClass(ZXXComposePictureViewCell.self, forCellWithReuseIdentifier:ZXXComposePictureCellReuseIdentifier )
        dataSource = self
        delegate = self
//       cell 的布局
        
        let margin: CGFloat = 5
        let cellWH = (SCREENWIDTH - 20 - margin * 2) / 3
        // 获取layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        // 设置itemSize
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        // 设置垂直间距和水平间距
        layout.minimumLineSpacing = margin
        layout.minimumInteritemSpacing = margin

    }
    // MARK: - 数组添加数据的方法
    func addImages(image: UIImage){
        imagebgArray.insert(image, atIndex: 0)
        if imagebgArray.count > 9{
            imagebgArray.removeLast()
        }
        
        // 判断images.count 如果大于 0
        if imagebgArray.count > 0{
            self.hidden = false
        }
        // 刷新UI
        reloadData()
    }
    
}
    
 //  MARK: -  数据源方法
extension ZXXComposePictureView:UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        /*
         - 当我们图片数组中 如果有 0  || 9  images.count
         - 当我们图片数组中 不是 0 也不是 9 的 images.count + 1
         */
        let count = imagebgArray.count
        if count == 0 || count == 9{
            return count
        }else{
            return count + 1
        }
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ZXXComposePictureCellReuseIdentifier, forIndexPath: indexPath) as! ZXXComposePictureViewCell
        
        /*
         会帮我们创建4个cell   indexPath.item == 3
         0     1    2
         [图片1, 图片2, 图片3] , 加号图片 [图片1, 图片2, 图片3].count = 3
         */
        
        if indexPath.item == imagebgArray.count{
            cell.image = nil
        }else{
            cell.image = imagebgArray[indexPath.item]
        }
        
        cell.composePictureViewCellClosure = {[weak self] in
//            print("删除按钮点击了")
            self?.imagebgArray.removeAtIndex(indexPath.item)
            if self?.imagebgArray.count <= 0{
                self?.hidden = true
            }
            self?.reloadData()
        }
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        if indexPath.item == imagebgArray.count{
            ComposePictureViewClosure?()
        }
    }

}
